﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallJob.Data;
using SmallJob.Data.Interfaces;
using SmallJob.Model;
using SmallShop.Services.Interfaces;

namespace SmallShop.Services.Implements
{
    public class FeatureService : RepositoryBase<Feature>, IFeatureService
    {
        public FeatureService(IDatabaseFactory DbFactory) : base(DbFactory) { }
        public bool IsExisted(string title)
        {
            var feat = from f in SmallJobDB.Features
                       where f.Title == title
                       select f;
            return (feat.Count() != 0);
        }
        public int Count()
        {
            return (from f in SmallJobDB.Features
                    select f).Count();
        }
        public override Feature GetById(int Id)
        {
            return (from f in SmallJobDB.Features
                    where f.ID == Id
                    select f).FirstOrDefault();
        }
        public override IEnumerable<Feature> All()
        {
            return (from f in SmallJobDB.Features
                    where !f.IsDeleted
                    orderby f.Order
                    select f).ToList();
        }
        public void AddFeatureItem(FeatureItem fi)
        {
            var a = !IsFeatureItemExist(fi);
            if (!IsFeatureItemExist(fi))
            {
                SmallJobDB.FeatureItems.Add(fi);
                SmallJobDB.Commit();
            }
        }
        public bool IsFeatureItemExist(FeatureItem fi)
        {
            var featureItem = (from f in SmallJobDB.FeatureItems
                               where fi.ProvideID == f.ProvideID && fi.FeatureID == f.FeatureID
                               select f).FirstOrDefault();
            return featureItem != null;
        }
        public bool IsFeatureItemExist(int fID, int itemID)
        {
            var featureItem = (from f in SmallJobDB.FeatureItems
                               where itemID == f.ProvideID && fID == f.FeatureID
                               select f).FirstOrDefault();
            return featureItem != null;
        }
        public List<FeatureItem> GetFeatureItems(int fID)
        {
            return (from f in SmallJobDB.FeatureItems
                    where fID == f.FeatureID
                    select f).ToList();
        }
        public void RemoveFeatureItem(int id)
        {
            var fi = (from f in SmallJobDB.FeatureItems
                      where f.ID == id
                      select f).FirstOrDefault();
            if (fi != null)
            {
                SmallJobDB.FeatureItems.Remove(fi);
                SmallJobDB.Commit();
            }
        }
    }
}
